package com.bianguo.android.eventbuslibrary;

import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.bianguo.android.eventbuslibrary.enums.NetworkState;
import com.bianguo.android.eventbuslibrary.utils.MLog;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class NetworkCallBack extends ConnectivityManager.NetworkCallback {

    @Override
    public void onAvailable(Network network) {
        super.onAvailable(network);
        MLog.i("网络连接");
        NetworkManager.getDefault().post(NetworkState.AUTO);
    }

    @Override
    public void onLost(Network network) {
        super.onLost(network);
        MLog.i("网络断开");
        NetworkManager.getDefault().post(NetworkState.NONE);
    }

    @Override
    public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
        super.onCapabilitiesChanged(network, networkCapabilities);
        if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
            if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                MLog.i("WIFI");
                NetworkManager.getDefault().post(NetworkState.WIFI);
            } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                MLog.i("移动蜂窝 cellular");
            }
        }
    }
}
